package com.sy.service.impl;

import com.sy.dto.UserSession;
import com.sy.entity.Admin;
import com.sy.entity.Student;
import com.sy.entity.Teacher;
import com.sy.exception.BusinessException;
import com.sy.utils.RandomUtils;
import com.sy.vo.ChangePwdVo;
import org.apache.commons.lang.StringUtils;
import com.sy.service.LoginService;
import com.sy.vo.LoginVo;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.logging.Handler;

import static com.sy.constant.RegConstant.STRONG_PWD_REGEX;
import static com.sy.constant.SystemConstant.*;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.UNAUTHORIZED;

@Service
public class LoginServiceImpl extends BaseServiceImpl implements LoginService {
    private String validateCode;

    @Override
    public UserSession login(LoginVo loginVo) {
        UserSession userSession = new UserSession();
        String account = loginVo.getAccount();
        String password = loginVo.getPassword();
        String identity = loginVo.getIdentity();
//        String autoLogin = loginVo.getAutoLogin();
        if (StringUtils.isBlank(account)) {
            buildError(BAD_REQUEST, "账号不能为空！");
        }
        if (StringUtils.isBlank(password)) {
            buildError(BAD_REQUEST, "密码不能为空！");
        }
        Student student = studentMapper.getStudentByAccount(account);
        Teacher teacher = teacherMapper.getTeacherByAccount(account);
        Admin admin = adminMapper.getAdminByAccount(account);

        //从student、teacher、admin三个表中利用account查找是否存在用户
        if (Objects.isNull(student) && Objects.isNull(teacher) && Objects.isNull(admin)) {
            buildError(UNAUTHORIZED, "用户不存在！");
        } else {
            //如果存在用户，判断身份
            //将前端传过来的身份码与定义的常量IDENTITY_STUDENT(学生)进行匹配
            if (IDENTITY_STUDENT.equals(identity)) {
                if (Objects.isNull(student)) {
                    buildError(UNAUTHORIZED, "身份不正确！");
                } else {
                    //如果身份匹配，将前端传过来的密码与数据库查找的用户的密码进行匹配
                    if (!StringUtils.equals(student.getPassword(), password)) {
                        buildError(UNAUTHORIZED, "密码错误！");
                    } else {
                        //当账户与密码都匹配时，将得到的用户id和identity添加到UserSession对象中
                        userSession.setId(student.getId());
                        userSession.setIdentity(identity);
                        userSession.setName(student.getName());
                        student.setPassword(null);
                    }
                }
            }

            //将前端传过来的身份码与定义的常量IDENTITY_TEACHER(老师)进行匹配
            if (IDENTITY_TEACHER.equals(identity)) {
                if (Objects.isNull(teacher)) {
                    buildError(UNAUTHORIZED, "身份不正确！");
                } else {
                    if (!StringUtils.equals(teacher.getPassword(), password)) {
                        buildError(UNAUTHORIZED, "密码错误！");
                    } else {
                        userSession.setId(teacher.getId());
                        userSession.setIdentity(identity);
                        userSession.setName(teacher.getName());
                        teacher.setPassword(null);
                    }
                }
            }

            //将前端传过来的身份码与定义的常量IDENTITY_ADMIN(管理员)进行匹配
            if (IDENTITY_ADMIN.equals(identity)) {
                if (Objects.isNull(admin)) {
                    buildError(UNAUTHORIZED, "身份不正确！");
                } else {
                    if (!StringUtils.equals(admin.getPassword(), password)) {
                        buildError(UNAUTHORIZED, "密码错误！");
                    } else {
                        userSession.setId(admin.getId());
                        userSession.setIdentity(identity);
                        userSession.setName(admin.getName());
                        admin.setPassword(null);
                    }
                }
            }
        }
        return userSession;
    }

    @Override
    public boolean changePwd(ChangePwdVo changePwdVo) {
        String id = changePwdVo.getId();
        String identity = changePwdVo.getIdentity();
        String oldPwd = changePwdVo.getOldPwd();
        String newPwd = changePwdVo.getNewPwd();

        try {
            if (StringUtils.isBlank(id)) {
                buildError(BAD_REQUEST, "ID不能为空！");
            }
            if (StringUtils.isBlank(identity)) {
                buildError(BAD_REQUEST, "身份码不能为空！");
            }
            if (StringUtils.isBlank(oldPwd)) {
                buildError(BAD_REQUEST, "旧密码不能为空！");
            }
            if (StringUtils.isBlank(newPwd)) {
                buildError(BAD_REQUEST, "新密码不能为空！");
            }
            if (!newPwd.matches(STRONG_PWD_REGEX)) {
                buildError(BAD_REQUEST, "新密码不符合强密码！");
            }
            if (StringUtils.equals(IDENTITY_TEACHER, identity)) {
                String oldTeaPwd = teacherMapper.getTeacherById(id).getPassword();
                if (!StringUtils.equals(oldPwd, oldTeaPwd)) {
                    buildError(BAD_REQUEST, "旧密码错误！");
                }
            }
            if (StringUtils.equals(IDENTITY_STUDENT, identity)) {
                String oldStuPwd = studentMapper.getStudentById(id).getPassword();
                if (!StringUtils.equals(oldPwd, oldStuPwd)) {
                    buildError(BAD_REQUEST, "旧密码错误！");
                }
            }
            if (StringUtils.equals(IDENTITY_ADMIN, identity)) {
                String oldAdminPwd = adminMapper.getAdminById(id).getPassword();
                if (!StringUtils.equals(oldPwd, oldAdminPwd)) {
                    buildError(BAD_REQUEST, "旧密码错误！");
                }
            }
            //修改密码
             if(!teacherMapper.changeTeaPwd(newPwd, id)){
                 throw new BusinessException(HttpStatus.NOT_FOUND,"修改密码失败");
             }
        } catch (Exception e) {
            throw new BusinessException(HttpStatus.NOT_FOUND,e.getMessage());
        }
        return true;
    }
}
